package cn.seaboot.admin.images.web;

import cn.seaboot.admin.images.bean.SysIcon;
import cn.seaboot.admin.images.bean.SysImage;
import cn.seaboot.admin.images.service.SysIconService;
import cn.seaboot.admin.mvc.Pagination;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.commons.file.Base64ImageUtils;
import cn.seaboot.commons.file.ResponseWriter;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * 创建文字图标
 *
 * @author Mr.css
 * @version 2020-09-30 20:01:38
 */
@Controller
@Tag(name = "系统文字图标管理")
@RequestMapping(value = "/sys/icon")
public class SysIconCtrl {

    @Resource
    private SysIconService sysIconService;


    /**
     * 预览图标
     * <p>
     * 将 base64 转码，变成可以直接查看的图片，
     * <p>
     * 这多少有些浪费性能，前端最好提供一定的策略，缓存后台的数据
     *
     * @param id id
     */
    @Operation(description = "图标 [t_image_icon] 查")
    @RequestMapping(value = "ref/{id}", method = RequestMethod.GET)
    public void ref(@PathVariable Long id, HttpServletResponse response) throws IOException {
        SysIcon icon = sysIconService.queryById(id);
        if (icon == null) {
            response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        } else {
            ResponseWriter.setMimeFromName(response, icon.getIconName());
            ResponseWriter.setInlineName(response, icon.getIconName());
            Base64ImageUtils.base64ToImage(icon.getContent(), response.getOutputStream());
        }
    }


    /**
     * 创建文字图标
     * <p>
     * 这个功能目前可以纯前端实现，用不上后台的算法
     *
     * @param txt      字符串
     * @param color    背景颜色
     * @param fontSize 字体大小
     * @param width    宽度
     * @param height   高度
     * @return -
     * @throws IOException -
     */
    @ResponseBody
    @Operation(description = "创建文字图标", parameters = {
            @Parameter(name = "color", description = "背景颜色(#3388FF)"),
            @Parameter(name = "fontSize", description = "字体大小(24)"),
            @Parameter(name = "width", description = "宽度(100)"),
            @Parameter(name = "height", description = "高度(100)")
    })
    @RequestMapping(value = "gen", method = RequestMethod.POST)
    public SysIcon create(@RequestParam String txt,
                          @RequestParam(defaultValue = "#ff851b") String color,
                          @RequestParam(defaultValue = "24") Integer fontSize,
                          @RequestParam(defaultValue = "100") Integer width,
                          @RequestParam(defaultValue = "100") Integer height) throws IOException {
        return sysIconService.create(txt, color, fontSize, width, height);
    }

    /**
     * 图片上传
     *
     * @param file 文件
     * @return SysImage
     * @throws IOException 文件未找到
     */
    @ResponseBody
    @RequestMapping(value = "upload", method = RequestMethod.POST)
    @Operation(description = "图片 [t_image_icon] 上传文件")
    public SysIcon upload(@RequestParam MultipartFile file,
                          @RequestParam(defaultValue = "0") Integer type) throws IOException {
        return sysIconService.upload(type, file);
    }

    /**
     * 下载图标
     *
     * @param params   过滤参数
     * @param response -
     * @throws IOException -
     */
    @ResponseBody
    @RequestMapping(value = "download", method = RequestMethod.GET)
    @Operation(description = "图片 [t_image_icon] 上传文件")
    public void download(@RequestParam Map<String, Object> params,
                         HttpServletResponse response) throws IOException {
        sysIconService.download(params, response);
    }

    /**
     * 图标 - 增
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "图标 [t_image_icon] 增")
    @RequestMapping(value = "info", method = RequestMethod.POST)
    public int saveInfo(@Validated SysIcon pojo) {
        return sysIconService.insert(pojo);
    }

    /**
     * 图标 - 删
     *
     * @param id id
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "图标 [t_image_icon] 删")
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public int deleteInfo(@Params Long id) {
        return sysIconService.deleteById(id);
    }

    /**
     * 图标 - 查
     *
     * @param id id
     * @return pojo
     */
    @ResponseBody
    @Operation(description = "图标 [t_image_icon] 查")
    @RequestMapping(value = "info", method = RequestMethod.GET)
    public SysIcon queryById(@Params Long id) {
        return sysIconService.queryById(id);
    }

    /**
     * 图标 - 改
     *
     * @param pojo pojo
     * @return affected rows
     */
    @ResponseBody
    @Operation(description = "图标 [t_image_icon] 改")
    @RequestMapping(value = "info", method = RequestMethod.PUT)
    public int updateInfo(@Validated SysIcon pojo, @Params String id) {
        return sysIconService.updateById(pojo);
    }

    /**
     * 图标 - 查列表
     *
     * @param params 参数
     * @return PageInfo
     */
    @Operation(description = "图标 [t_image_icon] 列表查询")
    @Parameters({@Parameter(name = "page", description = "页号"),
            @Parameter(name = "limit", description = "行数"),
            @Parameter(name = "sort", description = "排序")})
    @ResponseBody
    @RequestMapping(value = "page", method = RequestMethod.GET)
    public PageInfo<SysIcon> queryPage(@RequestParam Map<String, Object> params) {
        return Pagination.apply(params, map -> sysIconService.queryList(map));
    }
}
